// popup.js
// document.getElementById('openLocal').addEventListener('click', () => {
//     chrome.tabs.create({ url: 'https://example.com' });
// });

initTokenShow();

// 获取存储在chrome.storage.local中的数据
function initTokenShow(){
    chrome.storage.local.get(["DEV_F_HT","DEV_B_HT","TEST_F_HT","TEST_B_HT", "DEV_F_BQ","DEV_B_BQ","TEST_F_BQ","TEST_B_BQ"], function(result) {
        document.getElementById('dataList').innerHTML = '' ;
        let i = 0;
        if(result.DEV_F_HT){
            i = showItemHandle(result.DEV_F_HT, 'DEV_F_HT', '航天开发环境前台', i++);
        }
        if(result.DEV_B_HT){
            i = showItemHandle(result.DEV_B_HT, 'DEV_B_HT', '航天开发环境后台', i++);
        }
        if(result.TEST_F_HT){
            i = showItemHandle(result.TEST_F_HT, 'TEST_F_HT', '航天测试环境前台', i++);
        }
        if(result.TEST_B_HT){
            i = showItemHandle(result.TEST_B_HT, 'TEST_F_HT', '航天测试环境后台', i++);
        }
        if(result.DEV_F_BQ){
            i = showItemHandle(result.DEV_F_BQ, 'DEV_F_BQ', '兵器开发环境前台', i++);
        }
        if(result.DEV_B_BQ){
            i = showItemHandle(result.DEV_B_BQ, 'DEV_B_BQ', '兵器开发环境后台', i++);
        }
        if(result.TEST_F_BQ){
            i = showItemHandle(result.TEST_F_BQ, 'TEST_F_BQ', '兵器测试环境前台', i++);
        }
        if(result.TEST_B_BQ){
            i = showItemHandle(result.TEST_B_BQ, 'TEST_B_BQ', '兵器测试环境后台', i++);
        }
        
        document.getElementById('dataList').addEventListener('click', function (event) {
            if(event.target.nodeName == 'SPAN'){
                // console.log(event.target.userId, event.target.token)
                let cacheKey = event.target.cacheKey;
                let token = event.target.token;
                let userId = event.target.userId;
                if(cacheKey){
                    chrome.storage.local.get([cacheKey], function(result) {
                        console.log(result, cacheKey, token);
                        const newArray = result[cacheKey].filter(obj => obj.token !== token);
                        result[cacheKey] = newArray;
                        chrome.storage.local.set(result).then(() => {
                            initTokenShow();
                            // console.log('更新token记录成功：userId ' + token.userId + ' | token ' + token.token + ' | env ' + token.env);
                        });
                    })
                }
            }
        })
    });
}


function showItemHandle(array, cacheKey, title, j){
    if(!array || array.length === 0){
        return;
    }
    array.sort((a, b) => {
        let at = a.time || 0;
        let bt = b.time || 0;
        return bt - at;
    });
    let popupElement = document.getElementById('dataList');
    for (let i = 0; i < array.length; i++) {
        popupElement.appendChild(getShowItem(array[i], i==0 ? title : '------------------------', j++, cacheKey));
    }
    return j;
}

function getShowItem(item, env, i, cacheKey){
    let optionElement = document.createElement('li');
    optionElement.textContent = env +' | '+item.userId+' | ' + item.token + ' | ' + (new Date(item.time)).toLocaleString('zh-CN');
    optionElement.value = item.token;
    optionElement.userId = item.userId;
    optionElement.token = item.token;
    optionElement.className = 'op'

    let removeButton = document.createElement('span');
    removeButton.textContent = '  移除';
    removeButton.value = item.token;
    removeButton.style = 'cursor: pointer;';
    removeButton.userId = item.userId;
    removeButton.token = item.token;
    removeButton.cacheKey = cacheKey;

    optionElement.userId = item.userId;
    optionElement.env = env;
    optionElement.appendChild(removeButton);
    return optionElement;
}
